package com.wenli.lt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wenli.lt.entity.Goods;
import com.wenli.lt.entity.Information;
import com.wenli.lt.entity.Orders;
import com.wenli.lt.mapper.GoodsMapper;
import com.wenli.lt.mapper.InformationMapper;
import com.wenli.lt.mapper.OrdersMapper;
import com.wenli.lt.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lt
 * @since 2021-04-03
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    InformationMapper informationMapper;
    @Override
    public Boolean addOrders(Orders orders,String informationInfo,String sendStudentNumber) {
       String uuid = UUID.randomUUID().toString().replace("-","");
        orders.setOrderId(uuid);
        if (orders.getStatus() == 1 && !StringUtils.isBlank(informationInfo) && !StringUtils.isBlank(sendStudentNumber)){
            Goods goods1 = goodsMapper.selectById(orders.getGoodsId());
            Information information = new Information();
            information.setIsRead(0);
            information.setGoodsId(orders.getGoodsId());
            information.setSendStudentNumber(sendStudentNumber);
            information.setInformationInfo(informationInfo);
            information.setInformationId(UUID.randomUUID().toString().replace("-",""));
            information.setBeSendStudentNumber(goods1.getSellerstudentNumber());
            informationMapper.insert(information);
        }
//        orders.setStatus(0);
        return 1==  ordersMapper.insert(orders);
    }


    @Override
    public Boolean updateOrders(String ordersId, Integer stauts,String informationInfo,String sendStudentNumber) {
        Orders orders = ordersMapper.selectById(ordersId);
        Goods goods1 = goodsMapper.selectById(orders.getGoodsId());
        orders.setStatus(stauts);
        if (stauts == 1 && !StringUtils.isBlank(informationInfo) && !StringUtils.isBlank(sendStudentNumber)){
            Information information = new Information();
            information.setIsRead(0);
            information.setGoodsId(orders.getGoodsId());
            information.setSendStudentNumber(sendStudentNumber);
            information.setInformationInfo(informationInfo);
            information.setInformationId(UUID.randomUUID().toString().replace("-",""));
            information.setBeSendStudentNumber(goods1.getSellerstudentNumber());
            informationMapper.insert(information);
        }
       //如果订单 购买 成功  商品数量减1
        if (stauts == 2){
            Goods goods = goodsMapper.selectById(orders.getGoodsId());
            if (goods.getNoSoldCount() == 0){
                return false;
            }else {
                goods.setNoSoldCount(goods.getNoSoldCount() - 1);
                goods.setIsSoldOut(goods.getIsSoldOut() + 1);
                if (goods.getNoSoldCount() == 0){
                    //售空
                    goods.setIsSoldOut(1);
                }
                goodsMapper.updateById(goods);
                return  1== ordersMapper.updateById(orders);

            }

        }else {
            return  1== ordersMapper.updateById(orders);

        }

    }

    @Override
    public boolean deleteOrders(String ordersId) {
        return 1 == ordersMapper.deleteById(ordersId);
    }
}
